package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.YonbipFileUtils;
import com.ruoyi.common.utils.YonbipTokenUtil;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 凭证主表Controller
 * 
 * @author ruoyi
 * @date 2025-09-15
 */
@Controller
@RequestMapping("/system/glVoucher")
public class GlVoucherController extends BaseController
{
    private String prefix = "system/glVoucher";

    @Autowired
    private IGlVoucherService glVoucherService= SpringUtils.getBean(IGlVoucherService.class);;
    private IOrgAccbookService orgAccbookService=SpringUtils.getBean(IOrgAccbookService.class);
    private IYonbipCorrespondingAuxiliaryService yonbipCorrespondingAuxiliaryService=SpringUtils.getBean(IYonbipCorrespondingAuxiliaryService.class);
    private IYonbipKmysService yonbipKmysService=SpringUtils.getBean(IYonbipKmysService.class);
    private IYonbipCorrespondingAuxiliaryItemsService yonbipCorrespondingAuxiliaryItemsService=SpringUtils.getBean(IYonbipCorrespondingAuxiliaryItemsService.class);
    private IYonbipCashflowcaseService iYonbipCashflowcaseService=SpringUtils.getBean(IYonbipCashflowcaseService.class);
    private IOrgCqJkService orgCqJkService=SpringUtils.getBean(IOrgCqJkService.class);
    private IBdCustSupplierService bdCustSupplierService = SpringUtils.getBean(IBdCustSupplierService.class);
    private IGlDetailService glDetailService = SpringUtils.getBean(IGlDetailService.class);
    @RequiresPermissions("system:glVoucher:view")
    @GetMapping()
    public String glVoucher()
    {
        return prefix + "/glVoucher";
    }

    /**
     * 查询凭证主表列表
     */
    @RequiresPermissions("system:glVoucher:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GlVoucher glVoucher)
    {
        startPage();
        List<GlVoucher> list = glVoucherService.selectGlVoucherList(glVoucher);
        return getDataTable(list);
    }

    /**
     * 导出凭证主表列表
     */
    @RequiresPermissions("system:glVoucher:export")
    @Log(title = "凭证主表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GlVoucher glVoucher)
    {
        List<GlVoucher> list = glVoucherService.selectGlVoucherList(glVoucher);
        ExcelUtil<GlVoucher> util = new ExcelUtil<GlVoucher>(GlVoucher.class);
        return util.exportExcel(list, "凭证主表数据");
    }

    /**
     * 新增凭证主表
     */
    @RequiresPermissions("system:glVoucher:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存凭证主表
     */
    @RequiresPermissions("system:glVoucher:add")
    @Log(title = "凭证主表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GlVoucher glVoucher)
    {
        return toAjax(glVoucherService.insertGlVoucher(glVoucher));
    }

    /**
     * 修改凭证主表
     */
    @RequiresPermissions("system:glVoucher:edit")
    @GetMapping("/edit/{pkVoucher}")
    public String edit(@PathVariable("pkVoucher") String pkVoucher, ModelMap mmap)
    {
        GlVoucher glVoucher = glVoucherService.selectGlVoucherByPkVoucher(pkVoucher);
        mmap.put("glVoucher", glVoucher);
        return prefix + "/edit";
    }

    /**
     * 修改保存凭证主表
     */
    @RequiresPermissions("system:glVoucher:edit")
    @Log(title = "凭证主表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GlVoucher glVoucher)
    {
        return toAjax(glVoucherService.updateGlVoucher(glVoucher));
    }

    /**
     * 删除凭证主表
     */
    @RequiresPermissions("system:glVoucher:remove")
    @Log(title = "凭证主表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(glVoucherService.deleteGlVoucherByPkVouchers(ids));
    }
    public void ApiQueryGlVoucher(String newdate){
        Date date = DateUtil.parse(newdate);
    //    Date date = DateUtil.date();
        String year= String.valueOf(DateUtil.year(date));
        String   month= String.valueOf(DateUtil.month(date)+1);
            if(month.length()==1){
                month="0"+month ;
            }

        try
        {

            List<OrgAccbook> orgAccbooklist= orgAccbookService.selectOrgAccbookList(new OrgAccbook());

            for(OrgAccbook orgAccbook :orgAccbooklist){
                OrgCqJk orgCqJk=orgCqJkService.selectOrgCqJkByCode(orgAccbook.getCODE());
                GlVoucher glVoucher=new GlVoucher();
                glVoucher.setYEAR(year);
                glVoucher.setPERIOD(month);
                glVoucher.setPkOrg(orgAccbook.getPkRelorg());

                List<GlVoucher> glVoucherList=glVoucherService.selectGlVoucherList(glVoucher);
                List<String> lines=new ArrayList<>();
                List<String> linesSb=new ArrayList<>();
                //成功数
                int cnum=0;
                //失败数
                int snum=0;

                for( GlVoucher glVoucher1 : glVoucherList){

                    YonbipTokenUtil yonbipTokenUtil= new YonbipTokenUtil();
                    String apiuri = "nccloud/api/gl/voucher/detail";
                    JSONObject jsonObjectQuery=new JSONObject();
                    String[] orgs = new String[1];
                    orgs[0]=orgAccbook.getCODE();
                    jsonObjectQuery.set("accbookCode",orgs);
                    jsonObjectQuery.set("year",glVoucher1.getYEAR());
                    jsonObjectQuery.set("period",glVoucher1.getPERIOD());
                    jsonObjectQuery.set("num",glVoucher1.getNUM());

                    String requestParam =jsonObjectQuery.toString();

                   String result = yonbipTokenUtil.callAPI(apiuri, requestParam);
                  //  String jsonString = result.substring("result:".length());
                    JSONObject jsonObjectResult=JSONUtil.parseObj(result);
                   JSONArray jsonArrayData=JSONUtil.parseArray(jsonObjectResult.get("data"));
                    for(int i=0;i<jsonArrayData.size();i++){
                       JSONObject jsonObject= (JSONObject) jsonArrayData.get(i);
                        JSONArray jsonArrayDataDatailList=JSONUtil.parseArray(jsonObject.get("details"));
                        for(int datailsSize=0;datailsSize<jsonArrayDataDatailList.size();datailsSize++){
                            //c查询对应科目
                            JSONObject jsonArrayDataDatail=(JSONObject) jsonArrayDataDatailList.get(datailsSize);

                            YonbipKmys yonbipKmys= yonbipKmysService.selectYonbipKmysByKmbm(jsonArrayDataDatail.getStr("accountCode"));
                            if(StringUtils.isNull(yonbipKmys)){
                                linesSb.add("科目未找到,科目号:"+jsonArrayDataDatail.getStr("accountCode")+"凭证号:"+glVoucher1.getNUM()+"制单日期:"+glVoucher1.getPREPAREDDATE());
                                snum++;
                                continue;
                            }
                            String fzType="";
                            String fzValue="";
                            String fzTypePj="";
                            String fzValuePj="";
                            //借贷方向
                            String jd="";
                            //借贷金额
                            String monery="";
                            //数量
                            String num="";
                            if(jsonArrayDataDatail.getDouble("debitAmount")>0){
                                jd="0";
                                monery=jsonArrayDataDatail.getStr("debitAmount");
                                num=jsonArrayDataDatail.getStr("debitQuant");
                            }else if(jsonArrayDataDatail.getDouble("creditAmount")>0){
                                jd="1";
                                monery=jsonArrayDataDatail.getStr("creditAmount");
                                num=jsonArrayDataDatail.getStr("creditQuant");
                            }
                           //查询辅助项对应表
                            YonbipCorrespondingAuxiliary yonbipCorrespondingAuxiliary=new YonbipCorrespondingAuxiliary();
                            yonbipCorrespondingAuxiliary.setAccountcode(jsonArrayDataDatail.getStr("accountCode"));
                            List<YonbipCorrespondingAuxiliary> list = yonbipCorrespondingAuxiliaryService.selectYonbipCorrespondingAuxiliaryList(yonbipCorrespondingAuxiliary);
                            for(YonbipCorrespondingAuxiliary y:list){
                               if("1".equals(y.getIsAdd())){
                                   fzType+= y.getNewCode()+"|";
                                   fzValue+= y.getFixedValue()+"|";
                               }
                            }

                            if( StringUtils.isNotNull(jsonArrayDataDatail.getStr("assvos"))){
                                JSONArray assvos=JSONUtil.parseArray(jsonArrayDataDatail.get("assvos"));
                                for(int assvosSize=0;assvosSize<assvos.size();assvosSize++){
                                    JSONObject assvo= (JSONObject) assvos.get(assvosSize);
                                    YonbipCorrespondingAuxiliary yonbipCorrespondingAuxiliaryAss=new YonbipCorrespondingAuxiliary();
                                    yonbipCorrespondingAuxiliaryAss.setAccountcode(jsonArrayDataDatail.getStr("accountCode"));
                                    yonbipCorrespondingAuxiliaryAss.setName(assvo.getStr("m_checktypename"));
                                    YonbipCorrespondingAuxiliary yonbipCorrespondingAuxiliaryAssResult=yonbipCorrespondingAuxiliaryService.selectYonbipCorrespondingAuxiliaryByAccountCodeOldCode(yonbipCorrespondingAuxiliaryAss);
                                   if(StringUtils.isNull(yonbipCorrespondingAuxiliaryAssResult)){
                                       linesSb.add("辅助核算未找到,科目号:"+jsonArrayDataDatail.getStr("accountCode")+"辅助类型:"+assvo.getStr("m_checktypename")+"凭证号:"+glVoucher1.getNUM()+"制单日期:"+glVoucher1.getPREPAREDDATE());
                                       fzType+= ""+"|";
                                       fzValue+= ""+"|";
                                       continue;
                                   }
                                    if("1".equals(yonbipCorrespondingAuxiliaryAssResult.getIsFixed())){
                                        //辅助核算固定值
                                        fzType+= yonbipCorrespondingAuxiliaryAssResult.getNewCode()+"|";
                                        fzValue+= yonbipCorrespondingAuxiliaryAssResult.getFixedValue()+"|";
                                    }else if("1".equals(yonbipCorrespondingAuxiliaryAssResult.getIsConvert())){
                                           //辅助核算需要转换的数据
                                        YonbipCorrespondingAuxiliaryItems yonbipCorrespondingAuxiliaryItems=new YonbipCorrespondingAuxiliaryItems();
                                        yonbipCorrespondingAuxiliaryItems.setOldCode(yonbipCorrespondingAuxiliaryAssResult.getOldCode());
                                        yonbipCorrespondingAuxiliaryItems.setFzCode(assvo.getStr("m_checkvaluecode"));
                                        List<YonbipCorrespondingAuxiliaryItems> yonbipCorrespondingAuxiliaryItemslist=yonbipCorrespondingAuxiliaryItemsService.selectYonbipCorrespondingAuxiliaryItemsList(yonbipCorrespondingAuxiliaryItems);
                                        if(yonbipCorrespondingAuxiliaryItemslist.size()>0){
                                            fzType+= yonbipCorrespondingAuxiliaryAssResult.getNewCode()+"|";
                                            fzValue+= yonbipCorrespondingAuxiliaryItemslist.get(0).getZhCode()+"|";
                                        }
                                    }else{
                                        if("0004".equals(assvo.get("m_checktypecode"))){
                                            BdCustSupplier bdCustSupplier=new BdCustSupplier();
                                            bdCustSupplier.setPkGroup(orgAccbook.getPkGroup());
                                            bdCustSupplier.setCODE(assvo.getStr("m_checkvaluecode"));
                                            List<BdCustSupplier> bdCustSupplierlist =  bdCustSupplierService.selectBdCustSupplierList(bdCustSupplier);
                                            fzType+= yonbipCorrespondingAuxiliaryAssResult.getNewCode()+"|";
                                            if(!bdCustSupplierlist.isEmpty()){
                                            if(bdCustSupplierlist.size()>0){
                                            fzValue+= toAllUpperCase(bdCustSupplierlist.get(0).getTAXPAYERID())+"|";
                                            }else{
                                                linesSb.add("客商社会统一代码未找到"+assvo.getStr("m_checkvaluecode"));
                                                fzType+= ""+"|";
                                                fzValue+= ""+"|";
                                                continue;
                                             }
                                            }else{
                                                linesSb.add("客商社会统一代码未找到"+assvo.getStr("m_checkvaluecode"));
                                                fzType+= ""+"|";
                                                fzValue+= ""+"|";
                                                continue;
                                            }
                                        }else{
                                            //辅助核算不需要转换数据
                                            fzType += yonbipCorrespondingAuxiliaryAssResult.getNewCode()+ "|";
                                            fzValue += assvo.get("m_checkvaluecode") + "|";
                                        }
                                    }

                                }
                                for(int listSize=0;listSize<(10-assvos.size());listSize++){
                                    fzTypePj+=""+"|";
                                    fzValuePj+=""+"|";
                                }
                            }
                              //现金流量
                            String xjllType="";
                            String xjllValue="";
                            YonbipCashflowcase yonbipCashflowcase=iYonbipCashflowcaseService.selectYonbipCashflowcaseByPkDetail(jsonArrayDataDatail.getStr("pk_detail"));
                             if(StringUtils.isNotNull(yonbipCashflowcase)){
                                 YonbipCorrespondingAuxiliaryItems items=new YonbipCorrespondingAuxiliaryItems();
                                 items.setFzCode(yonbipCashflowcase.getCode());
                                 List<YonbipCorrespondingAuxiliaryItems> itemsList=yonbipCorrespondingAuxiliaryItemsService.selectYonbipCorrespondingAuxiliaryItemsList(items);
                                 if(itemsList.size()>0){
                                     xjllType=itemsList.get(0).getZhCode();
                                 }else{
                                     linesSb.add("现金流量未找到,科目号:"+yonbipCashflowcase.getCode()+"凭证号:"+glVoucher1.getNUM()+"制单日期:"+glVoucher1.getPREPAREDDATE());
                                     continue;
                                 }

                                 xjllValue=yonbipCashflowcase.getMoney();
                             }
                             String tALLYDATE="";
                            String verifydate="";
                             if(glVoucher1.getTALLYDATE().equals("~")){
                                 tALLYDATE=DateUtil.format(DateUtil.parse(glVoucher1.getTALLYDATE()), "yyyy-MM-dd");
                             }
                            if(glVoucher1.getTALLYDATE().equals("~")){
                                verifydate=DateUtil.format(DateUtil.parse(glVoucher1.getTALLYDATE()), "yyyy-MM-dd");
                            }
                            lines.add(orgCqJk.getJcode()+"|"+orgCqJk.getJcode()+"-0001"+"|"+glVoucher1.getPkVoucher()+"|"
                                   + glVoucher1.getNUM()+"|"+jsonObject.get("year")+"|"+jsonObject.get("period")+"|"
                                    + DateUtil.format(DateUtil.parse(jsonObject.getStr("prepareddate")), "yyyy-MM-dd")+"|"+jsonObject.get("vouchertype_code")+"|"+ glVoucher1.getATTACHMENT()+"|"
                                    + jsonObject.get("prepared_code")+"|"+jsonObject.get("casher_code")+"|"+jsonObject.get("checked_code")+"|"
                                    + tALLYDATE +"|"+jsonObject.get("manager_code")+"|"+jsonArrayDataDatail.get("detailIndex")+"|"

                                    + yonbipKmys.getBzhkmbm()+"|"+jsonObject.get("explanation")+"|"+jsonObject.get("price")+"|"
                                    // 数量                                    原币金额                           科目方向
                                    + num+"|"+monery+"|"+jd+"|"

                                                                            //业务日期                                汇率
                                    + "CNY"+"|"+verifydate+"|"+"1"+"|"
                                     //本币金额
                                    + monery+"|"
                                   //辅助类型
                                   +fzType+fzTypePj
                                    //辅助类型值
                                   +fzValue+fzValuePj
                                    //现金流量类型
                                    +xjllType+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"
                                    //现金流量值
                                     +xjllValue+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"
                                    //扩展字段
                                    +""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"

                            ) ;
                        }

                    }

                 //  lines.add(orgDept.getCode()+"|"+orgDept.getName()+"|"+orgCqJk.getJcode()+"|"+parentCode+"|"+orgDept.getTel()+"|"+orgDept.getEnablestate()) ;
                    cnum++;
                }
                YonbipFileUtils.createFileWithLines(
                        "C:/basedata",
                        newdate+"-"+orgCqJk.getJcode()+"-voucher.txt",
                        lines,
                        false
                );
                linesSb.add("凭证数据导入成功"+cnum+"条");
                linesSb.add("凭证数据导入失败"+snum+"条");
                YonbipFileUtils.createFileWithLines(
                        "C:/basedata",
                        newdate+"-"+orgCqJk.getJcode()+"-voucher.txt.ok",
                        linesSb,
                        false
                );
            }


        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    //起初查询
    public void ApiQueryQcGlVoucher(String newdate){
        Date date = DateUtil.parse(newdate);
        //    Date date = DateUtil.date();
        String year= String.valueOf(DateUtil.year(date));
        String   month= String.valueOf(DateUtil.month(date)+1);
        if(month.length()==1){
            month="0"+month ;
        }

        try
        {

            List<OrgAccbook> orgAccbooklist= orgAccbookService.selectOrgAccbookList(new OrgAccbook());

            for(OrgAccbook orgAccbook :orgAccbooklist){
                OrgCqJk orgCqJk=orgCqJkService.selectOrgCqJkByCode(orgAccbook.getCODE());
                GlVoucher glVoucher=new GlVoucher();
                glVoucher.setYEAR(year);
                glVoucher.setPERIOD("00");
                glVoucher.setPkOrg(orgAccbook.getPkRelorg());

                List<GlVoucher> glVoucherList=glVoucherService.selectGlVoucherQcList(glVoucher);
                List<String> lines=new ArrayList<>();
                List<String> linesSb=new ArrayList<>();
                //成功数
                int cnum=0;
                //失败数
                int snum=0;

                for( GlVoucher glVoucher1 : glVoucherList){

                    YonbipTokenUtil yonbipTokenUtil= new YonbipTokenUtil();
                    String apiuri = "nccloud/api/uapbd/jdfz/glVoucher/queryByPk";
                    JSONObject jsonObjectQuery=new JSONObject();
                    String[] orgs = new String[1];
                    orgs[0]=orgAccbook.getCODE();
                    jsonObjectQuery.set("pk_voucher",glVoucher1.getPkVoucher());
                    String requestParam =jsonObjectQuery.toString();

                    String result = yonbipTokenUtil.callAPI(apiuri, requestParam);
                    //  String jsonString = result.substring("result:".length());
                    JSONObject jsonObjectResult=JSONUtil.parseObj(result);
                    JSONArray jsonArrayData=JSONUtil.parseArray(jsonObjectResult.get("data"));
                    for(int i=0;i<jsonArrayData.size();i++){
                        JSONObject jsonObject= (JSONObject) jsonArrayData.get(i);
                        JSONArray jsonArrayDataDatailList=JSONUtil.parseArray(jsonObject.get("details"));
                        for(int datailsSize=0;datailsSize<jsonArrayDataDatailList.size();datailsSize++){
                            //c查询对应科目
                            JSONObject jsonArrayDataDatail=(JSONObject) jsonArrayDataDatailList.get(datailsSize);
                            String pk_detail=jsonArrayDataDatail.getStr("pk_detail");
                            String DetailAccountCode=glDetailService.selectGlDetailByPkDetail(jsonArrayDataDatail.getStr("pk_detail")).getACCOUNTCODE();
                            //YonbipKmys yonbipKmys= yonbipKmysService.selectYonbipKmysByKmbm(jsonArrayDataDatail.getStr("accountCode"));
                            YonbipKmys yonbipKmys= yonbipKmysService.selectYonbipKmysByKmbm(DetailAccountCode);
                            if(StringUtils.isNull(yonbipKmys)){
                                linesSb.add("科目未找到,科目号:"+DetailAccountCode+"凭证号:"+glVoucher1.getNUM()+"制单日期:"+glVoucher1.getPREPAREDDATE());
                                snum++;
                                continue;
                            }
                            String fzType="";
                            String fzValue="";
                            String fzTypePj="";
                            String fzValuePj="";
                            //借贷方向
                            String jd="";
                            //借贷金额
                            String monery="";
                            //数量
                            String num="";
                            if(jsonArrayDataDatail.getDouble("debitAmount")>0){
                                jd="0";
                                monery=jsonArrayDataDatail.getStr("debitAmount");
                                num=jsonArrayDataDatail.getStr("debitQuant");
                            }else if(jsonArrayDataDatail.getDouble("creditAmount")>0){
                                jd="1";
                                monery=jsonArrayDataDatail.getStr("creditAmount");
                                num=jsonArrayDataDatail.getStr("creditQuant");
                            }
                            //查询辅助项对应表
                            YonbipCorrespondingAuxiliary yonbipCorrespondingAuxiliary=new YonbipCorrespondingAuxiliary();
                            yonbipCorrespondingAuxiliary.setAccountcode(DetailAccountCode);
                            List<YonbipCorrespondingAuxiliary> list = yonbipCorrespondingAuxiliaryService.selectYonbipCorrespondingAuxiliaryList(yonbipCorrespondingAuxiliary);
                            for(YonbipCorrespondingAuxiliary y:list){
                                if("1".equals(y.getIsAdd())){
                                    fzType+= y.getNewCode()+"|";
                                    fzValue+= y.getFixedValue()+"|";
                                }
                            }

                            if( StringUtils.isNotNull(jsonArrayDataDatail.getStr("assvos"))){
                                JSONArray assvos=JSONUtil.parseArray(jsonArrayDataDatail.get("assvos"));
                                for(int assvosSize=0;assvosSize<assvos.size();assvosSize++){
                                    JSONObject assvo= (JSONObject) assvos.get(assvosSize);
                                    YonbipCorrespondingAuxiliary yonbipCorrespondingAuxiliaryAss=new YonbipCorrespondingAuxiliary();
                                    yonbipCorrespondingAuxiliaryAss.setAccountcode(DetailAccountCode);
                                    yonbipCorrespondingAuxiliaryAss.setName(assvo.getStr("m_checktypename"));
                                    YonbipCorrespondingAuxiliary yonbipCorrespondingAuxiliaryAssResult=yonbipCorrespondingAuxiliaryService.selectYonbipCorrespondingAuxiliaryByAccountCodeOldCode(yonbipCorrespondingAuxiliaryAss);
                                    if(StringUtils.isNull(yonbipCorrespondingAuxiliaryAssResult)){
                                        linesSb.add("辅助核算未找到,科目号:"+DetailAccountCode+"辅助类型:"+assvo.getStr("m_checktypename")+"凭证号:"+glVoucher1.getNUM()+"制单日期:"+glVoucher1.getPREPAREDDATE());
                                        fzType+= ""+"|";
                                        fzValue+= ""+"|";
                                        continue;
                                    }
                                    if("1".equals(yonbipCorrespondingAuxiliaryAssResult.getIsFixed())){
                                        //辅助核算固定值
                                        fzType+= yonbipCorrespondingAuxiliaryAssResult.getNewCode()+"|";
                                        fzValue+= yonbipCorrespondingAuxiliaryAssResult.getFixedValue()+"|";
                                    }else if("1".equals(yonbipCorrespondingAuxiliaryAssResult.getIsConvert())){
                                        //辅助核算需要转换的数据
                                        YonbipCorrespondingAuxiliaryItems yonbipCorrespondingAuxiliaryItems=new YonbipCorrespondingAuxiliaryItems();
                                        yonbipCorrespondingAuxiliaryItems.setOldCode(yonbipCorrespondingAuxiliaryAssResult.getOldCode());
                                        yonbipCorrespondingAuxiliaryItems.setFzCode(assvo.getStr("m_checkvaluecode"));
                                        List<YonbipCorrespondingAuxiliaryItems> yonbipCorrespondingAuxiliaryItemslist=yonbipCorrespondingAuxiliaryItemsService.selectYonbipCorrespondingAuxiliaryItemsList(yonbipCorrespondingAuxiliaryItems);
                                        if(yonbipCorrespondingAuxiliaryItemslist.size()>0){
                                            fzType+= yonbipCorrespondingAuxiliaryAssResult.getNewCode()+"|";
                                            fzValue+= yonbipCorrespondingAuxiliaryItemslist.get(0).getZhCode()+"|";
                                        }
                                    }else{
                                        if("0004".equals(assvo.get("m_checktypecode"))){
                                            BdCustSupplier bdCustSupplier=new BdCustSupplier();
                                         //   bdCustSupplier.setPkGroup(orgAccbook.getPkGroup());
                                            bdCustSupplier.setCODE(assvo.getStr("m_checkvaluecode"));
                                            List<BdCustSupplier> bdCustSupplierlist =  bdCustSupplierService.selectBdCustSupplierList(bdCustSupplier);
                                            fzType+= yonbipCorrespondingAuxiliaryAssResult.getNewCode()+"|";
                                            if(!bdCustSupplierlist.isEmpty()){
                                                if(bdCustSupplierlist.size()>0){
                                                    fzValue+= toAllUpperCase(bdCustSupplierlist.get(0).getTAXPAYERID())+"|";
                                                }else{
                                                    linesSb.add("客商社会统一代码未找到"+assvo.getStr("m_checkvaluecode"));
                                                    fzType+= ""+"|";
                                                    fzValue+= ""+"|";
                                                    continue;
                                                }
                                            }else{
                                                linesSb.add("客商社会统一代码未找到"+assvo.getStr("m_checkvaluecode"));
                                                fzType+= ""+"|";
                                                fzValue+= ""+"|";
                                                continue;
                                            }
                                        }else{
                                            //辅助核算不需要转换数据
                                            fzType += yonbipCorrespondingAuxiliaryAssResult.getNewCode()+ "|";
                                            fzValue += assvo.get("m_checkvaluecode") + "|";
                                        }
                                    }

                                }
                                for(int listSize=0;listSize<(10-assvos.size());listSize++){
                                    fzTypePj+=""+"|";
                                    fzValuePj+=""+"|";
                                }
                            }
                            //现金流量
                            String xjllType="";
                            String xjllValue="";
                            YonbipCashflowcase yonbipCashflowcase=iYonbipCashflowcaseService.selectYonbipCashflowcaseByPkDetail(jsonArrayDataDatail.getStr("pk_detail"));
                            if(StringUtils.isNotNull(yonbipCashflowcase)){
                                YonbipCorrespondingAuxiliaryItems items=new YonbipCorrespondingAuxiliaryItems();
                                items.setFzCode(yonbipCashflowcase.getCode());
                                List<YonbipCorrespondingAuxiliaryItems> itemsList=yonbipCorrespondingAuxiliaryItemsService.selectYonbipCorrespondingAuxiliaryItemsList(items);
                                if(itemsList.size()>0){
                                    xjllType=itemsList.get(0).getZhCode();
                                }else{
                                    linesSb.add("现金流量未找到,科目号:"+yonbipCashflowcase.getCode()+"凭证号:"+glVoucher1.getNUM()+"制单日期:"+glVoucher1.getPREPAREDDATE());
                                    continue;
                                }

                                xjllValue=yonbipCashflowcase.getMoney();
                            }
                            String tALLYDATE="";
                            String verifydate="";
                            if(glVoucher1.getTALLYDATE().equals("~")){
                                tALLYDATE=DateUtil.format(DateUtil.parse(glVoucher1.getTALLYDATE()), "yyyy-MM-dd");
                            }
                            if(glVoucher1.getTALLYDATE().equals("~")){
                                verifydate=DateUtil.format(DateUtil.parse(glVoucher1.getTALLYDATE()), "yyyy-MM-dd");
                            }
                            lines.add(orgCqJk.getJcode()+"|"+orgCqJk.getJcode()+"-0001"+"|"+glVoucher1.getPkVoucher()+"|"
                                    + glVoucher1.getNUM()+"|"+jsonObject.get("year")+"|"+jsonObject.get("period")+"|"
                                    + DateUtil.format(DateUtil.parse(jsonObject.getStr("prepareddate")), "yyyy-MM-dd")+"|"+jsonObject.get("vouchertype_code")+"|"+ glVoucher1.getATTACHMENT()+"|"
                                    + jsonObject.get("prepared_code")+"|"+jsonObject.get("casher_code")+"|"+jsonObject.get("checked_code")+"|"
                                    + tALLYDATE +"|"+jsonObject.get("manager_code")+"|"+jsonArrayDataDatail.get("detailIndex")+"|"

                                    + yonbipKmys.getBzhkmbm()+"|"+jsonObject.get("explanation")+"|"+jsonObject.get("price")+"|"
                                    // 数量                                    原币金额                           科目方向
                                    + num+"|"+monery+"|"+jd+"|"

                                    //业务日期                                汇率
                                    + "CNY"+"|"+verifydate+"|"+"1"+"|"
                                    //本币金额
                                    + monery+"|"
                                    //辅助类型
                                    +fzType+fzTypePj
                                    //辅助类型值
                                    +fzValue+fzValuePj
                                    //现金流量类型
                                    +xjllType+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"
                                    //现金流量值
                                    +xjllValue+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"
                                    //扩展字段
                                    +""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"+""+"|"

                            ) ;
                        }

                    }

                    //  lines.add(orgDept.getCode()+"|"+orgDept.getName()+"|"+orgCqJk.getJcode()+"|"+parentCode+"|"+orgDept.getTel()+"|"+orgDept.getEnablestate()) ;
                    cnum++;
                }
                YonbipFileUtils.createFileWithLines(
                        "C:/basedata",
                        "20241231"+"-"+orgCqJk.getJcode()+"-voucher.txt",
                        lines,
                        false
                );
                linesSb.add("凭证数据导入成功"+cnum+"条");
                linesSb.add("凭证数据导入失败"+snum+"条");
                YonbipFileUtils.createFileWithLines(
                        "C:/basedata",
                        "20241231"+"-"+orgCqJk.getJcode()+"-voucher.txt.ok",
                        linesSb,
                        false
                );
            }


        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    public static String toAllUpperCase(String str) {
        if (str == "~") {
            return "";
        }
        return str.toUpperCase();
    }
}
